import time
import requests

class ShanxinValidator:
    def __init__(self, num, id_list):
        self.num = num
        self.id_list = id_list
        self.params_list = []

    def process_519_error(self, problematic_params):
        # Put the params causing 519 error back to the end of the params_list
        self.params_list.remove(problematic_params)
        self.params_list.append(problematic_params)

    def process_518_error(self, params):
        # Remove the phone number from params and match a new phone number with ID
        self.num.remove(params['phone'])
        new_phone = self.num[(self.params_list.index(params) + 1) % len(self.num)]
        params['phone'] = new_phone
        self.params_list.append(params)

    def make_request(self, url, params):
        result = requests.get(url=url, params=params).json()
        return result

    def validate(self, url):
        for i in range(min(len(self.num), len(self.id_list))):
            params = {"phone": self.num[i], "shanxinId": self.id_list[i]}
            self.params_list.append(params)

        # Handle remaining elements
        remaining_id = self.id_list[len(self.params_list):]
        for id_value in remaining_id:
            params = {"phone": self.num[(len(self.params_list) + 1) % len(self.num)], "shanxinId": id_value}
            self.params_list.append(params)

        passid = []
        for params in self.params_list:
            result = self.make_request(url, params)

            if result['data']['code'] == '200':
                print("通过的编号:", params['shanxinId'], result['data']['code'])
                passid.append(params['shanxinId'])
            elif result['data']['code'] == '519':
                self.process_519_error(params)
                print("手机号入参时间间隔小于30s,放在后面重新验证")
                print(params['phone'], params['shanxinId'], result['data']['code'])
            elif result['data']['code'] == '518':
                self.process_518_error(params)
                print("手机号超频，该id重新匹配")
                print(params['phone'], params['shanxinId'], result['data']['code'])

            time.sleep(2.5)  # Request interval

        print("通过的列表如下：", passid)
        print("通过的元素个数如下：", len(passid))

        set_a = set(self.id_list)
        set_b = set(passid)

        if set_a == set_b:
            print("所有id全部验证通过")
        else:
            print("部分未验证通过，如下：")
            different_elements = set_a.symmetric_difference(set_b)
            print(list(different_elements))

def remove_duplicates_and_count(input_content):
    input_list = input_content.strip().split("\n")
    unique_list = list(set(map(int, input_list)))
    count = len(unique_list)
    return unique_list, count

if __name__ == "__main__":
    # 号码池
    num = ['13083404548', '18017870857', '13818441706', '19117356229', '13681868541', '17638822921', '15301814045',
           '17621231028', '18848965138', '17521620046', '17625931232', '16602817245', '17621090811', '18621804737',
           '13564883617', '17521219615', '15223431389']

    # Example usage:
    input_content = """449
    """
    unique_elements, count = remove_duplicates_and_count(input_content)
    print("Unique elements:", unique_elements)
    print("Number of unique elements:", count)

    command = input("查看控制台清洗的id信息，若上述的id无异常，请输入：0（代表执行）/非0（代表终止）")
    if command == "0":
        print("执行相应的操作")
        url = "https://wt-stg.zenggao.net/admin/parasol/public/api/sip/test/shanxin"
        validator = ShanxinValidator(num, remove_duplicates_and_count(input_content)[0])
        validator.validate(url)
    else:
        print("终止")
